from django.db.models import Sum
from django.forms import forms
from django.http import JsonResponse

from app02.utils.encrypt import md5
from app02.utils.pegination import Pegination
from django.shortcuts import render,redirect,HttpResponse
from django.views.decorators.csrf import csrf_exempt

from app02 import models

#教师首页
from app02.utils.bootrstap import BootrsapModelForm


def teacher_list(request):
    if request.method=='GET':
        return render(request,'teacher_view.html')



#创建ModelForm
class CourseModelForm(BootrsapModelForm):
    class Meta:
        #关联表
        model=models.CourseTable
        fields='__all__'
#课程管理列表
def teacher_course(request):
    if request.method=='GET':
        title='课程信息表'
        form_data=CourseModelForm()
        #搜索框
        seach_list = request.GET.get('t', '')
        print(seach_list)
        data_t={}
        #搜索框中内容对应sections节次
        if seach_list:
            data_t['sections__contains']=seach_list

        #获取用户提交的信息
        dest=models.CourseTable.objects.filter(**data_t)

        #分页
        page_object=Pegination(request,dest,page_size=6)
        context={
            'dest':page_object.page_mun,
            'page_string':page_object.html(),
            'seach_list':seach_list,
            'title': title,
            'form_data': form_data
        }

        return render(request,"teacher_course.html",context)


#编辑课表窗口
def teacher_EditGET(request):
    #获取id
    uid=request.GET.get('uid')
    print(uid)
    # 根据id获取课程信息表对应的那一列的数据
    teacher_obj=models.CourseTable.objects.filter(id=uid).values('sections','monday','tuesday','wednesday','thursday','friday').first()

    #判断数据存不存在
    if not teacher_obj:
        return JsonResponse({'static':False,'error':'数据不存在'})
    result={'static':True,'data':teacher_obj}
    return JsonResponse(result)

#编辑课程信息表
@csrf_exempt
def teacher_EditCourse(request):
    #根据id获取课程信息表对应的那一列的数据
    uid=request.GET.get('uid')
    # 根据id获取课程信息表对应的那一列的数据
    teacher_test=models.CourseTable.objects.filter(id=uid).first()
    if not teacher_test:
        return JsonResponse({'static':False,'tpis':'数据不存在'})
    form_data=CourseModelForm(data=request.POST,instance=teacher_test)
    if form_data.is_valid():
        form_data.save()
        return JsonResponse({'static':True})
    return HttpResponse(JsonResponse({'static':False,'error':form_data.errors}))

#删除课表
def teacher_delete(request):
      uid=request.GET.get('uid')
      exits=models.CourseTable.objects.filter(id=uid).exists()
      if not exits:
          return JsonResponse({'static':False,'error':'删除失败，数据不存在'})
      models.CourseTable.objects.filter(id=uid).delete()
      return JsonResponse({'static':True})


#班级管理列表
#创建ModelForm
class ClassInfoModelForm(BootrsapModelForm):
    class Meta:
        #关联表
        model=models.StudentInfo
        exclude=['pwd','h_type','teach']
def teacher_classinfo(request):
    if request.method=='GET':
        title='班级信息'
        form_data=ClassInfoModelForm()
        #搜索框
        seach_list = request.GET.get('t', '')
        print(seach_list)
        data_t={}
        # 搜索框中内容对应数据库中字段
        if seach_list:
            data_t['user__contains']=seach_list

        #获取用户提交的信息
        class_dest=models.StudentInfo.objects.filter(**data_t)

        #分页
        page_object=Pegination(request,class_dest,page_size=6)
        context={
            'class_dest':page_object.page_mun,
            'page_string':page_object.html(),
            'seach_list':seach_list,
            'title': title,
            'form_data': form_data
        }

        return render(request,"teacher_Classinfo.html",context)

#新建班级人员信息
@csrf_exempt
def teacher_addClassInfo(request):
    form_data=ClassInfoModelForm(data=request.POST)
    if form_data.is_valid():
        form_data.save()
        data_dict={'static':True}
        return  JsonResponse(data_dict)
    return JsonResponse({'static':False,'error':form_data.errors})


#编辑班级信息窗口
def teacher_ClassInfoEditGET(request):
    #获取id
    uid=request.GET.get('uid')
    # 根据id获取学生信息表对应的那一列的数据
    teacher_classinfoObject=models.StudentInfo.objects.filter(id=uid).values('id','user','StudentID','Studentage','Studentsex','Studentmodlie').first()

    #判断数据存不存在
    if not teacher_classinfoObject:
        return JsonResponse({'static':False,'error':'数据不存在'})
    result={'static':True,'data':teacher_classinfoObject}
    return JsonResponse(result)

#编辑班级人员信息表
@csrf_exempt
def teacher_EditClassInfo(request):
    #根据id获取课程信息表对应的那一列的数据
    uid=request.GET.get('uid')
    # 根据id获取课程信息表对应的那一列的数据
    teacher_test=models.StudentInfo.objects.filter(id=uid).first()
    if not teacher_test:
        return JsonResponse({'static':False,'tpis':'数据不存在'})
    form_data=ClassInfoModelForm(data=request.POST,instance=teacher_test)
    if form_data.is_valid():
        form_data.save()
        return JsonResponse({'static':True})
    return HttpResponse(JsonResponse({'static':False,'error':form_data.errors}))

#删除学生
def teacher_deleteClassInfo(request):
      uid=request.GET.get('uid')
      exits=models.StudentInfo.objects.filter(id=uid).exists()
      if not exits:
          return JsonResponse({'static':False,'error':'删除失败，数据不存在'})
      models.StudentInfo.objects.filter(id=uid).delete()
      return JsonResponse({'static':True})


#请假管理
#学生请假信息审核
#创建ModelForm
class StudentLeaveSModelForm(BootrsapModelForm):
    class Meta:
        #关联表
        model=models.StudentLeave
        exclude=['admins','exam']

#请假信息管理
def teacher_StudentLeave(request):
    if request.method == 'GET':
        title = '请假信息'
        form_data =StudentLeaveSModelForm()
        # 搜索框
        seach_list = request.GET.get('q', '')
        data_t = {}
        # 搜索框中内容对应数据库中字段
        if seach_list:
            data_t['name__contains'] = seach_list

        # 获取用户提交的信息
        student_dest = models.StudentLeave.objects.filter(**data_t)

        # 分页
        page_object = Pegination(request, student_dest, page_size=6)
        context = {
            'student_dest': page_object.page_mun,
            'page_string': page_object.html(),
            'seach_list': seach_list,
            'title': title,
            'form_data': form_data
        }
        return render(request, "teacher_StudentLeave.html", context)

#审核学生请假信息窗口
def teacher_StudentLeaveEditGET(request):
    #获取id
    uid=request.GET.get('uid')
    # 根据id获取学生信息表对应的那一列的数据
    leave_Object=models.StudentLeave.objects.filter(id=uid).values('id','name','sex','age','xueID','test','times','retime','level','teachers','teacher_exam').first()

    #判断数据存不存在
    if not leave_Object:
        return JsonResponse({'static':False,'error':'数据不存在'})
    result={'static':True,'data':leave_Object}
    return JsonResponse(result)

#编辑学生请假信息表
@csrf_exempt
def teacher_studentedit(request):
    #根据id获取课程信息表对应的那一列的数据
    uid=request.GET.get('uid')
    # 根据id获取课程信息表对应的那一列的数据
    leave_test=models.StudentLeave.objects.filter(id=uid).first()
    print(leave_test)
    if not leave_test:
        return JsonResponse({'static':False,'tpis':'数据不存在'})
    form_data=StudentLeaveSModelForm(data=request.POST,instance=leave_test)
    if form_data.is_valid():
        form_data.save()
        return JsonResponse({'static':True})
    return HttpResponse(JsonResponse({'static':False,'error':form_data.errors}))


#教师请假管理
class TeacherLeaveModelForm(BootrsapModelForm):
    class Meta:
        #关联表
        model=models.TeacherLeave
        exclude=['admins','exam']
def teacher_leave(request):
    if request.method == 'GET':
        title = '请假信息'
        form_data =TeacherLeaveModelForm()
        # 搜索框
        seach_list = request.GET.get('q', '')
        data_t = {}
        # 搜索框中内容对应数据库中字段
        if seach_list:
            data_t['name__contains'] = seach_list

        # 获取用户提交的信息
        teacher_dest = models.TeacherLeave.objects.filter(**data_t)

        # 分页
        page_object = Pegination(request, teacher_dest, page_size=6)
        context = {
            'teacher_dest': page_object.page_mun,
            'page_string': page_object.html(),
            'seach_list': seach_list,
            'title': title,
            'form_data': form_data
        }
        return render(request, "teacher_leave.html", context)

#新建请假信息
@csrf_exempt
def teacher_leaveAdd(request):
    form_data=TeacherLeaveModelForm(data=request.POST)
    if form_data.is_valid():
        form_data.save()
        data_dict={'static':True}
        return  JsonResponse(data_dict)
    return JsonResponse({'static':False,'error':form_data.errors})

#销假窗口
def teacher_LeaveReporting(request):
    #获取id
    uid=request.GET.get('uid')
    # 根据id获取学生信息表对应的那一列的数据
    leave_Object=models.TeacherLeave.objects.filter(id=uid).values('id','name','sex','age','teacherID','test','times','retime','level').first()

    #判断数据存不存在
    if not leave_Object:
        return JsonResponse({'static':False,'error':'数据不存在'})
    result={'static':True,'data':leave_Object}
    return JsonResponse(result)

#销假
@csrf_exempt
def teacher_reporting(request):
    #根据id获取课程信息表对应的那一列的数据
    uid=request.GET.get('uid')
    # 根据id获取课程信息表对应的那一列的数据
    leave_test=models.TeacherLeave.objects.filter(id=uid).first()
    if not leave_test:
        return JsonResponse({'static':False,'tpis':'数据不存在'})
    form_data=TeacherLeaveModelForm(data=request.POST,instance=leave_test)
    if form_data.is_valid():
        form_data.save()
        return JsonResponse({'static':True})
    return HttpResponse(JsonResponse({'static':False,'error':form_data.errors}))

#删除请假
def teacher_deleteLeave(request):
      uid=request.GET.get('uid')
      exits=models.TeacherLeave.objects.filter(id=uid).exists()
      if not exits:
          return JsonResponse({'static':False,'error':'删除失败，数据不存在'})
      models.TeacherLeave.objects.filter(id=uid).delete()
      return JsonResponse({'static':True})


#学生成绩管理
class TeacherScoreModelForm(BootrsapModelForm):
    class Meta:
        #关联表
        model=models.StudentScore
        exclude=['total']
def teacher_StudentScore(request):
    if request.method == 'GET':
        title = '成绩信息表'
        form_data =TeacherScoreModelForm()
        # 搜索框
        seach_list = request.GET.get('q', '')
        data_t = {}
        # 搜索框中内容对应数据库中字段
        if seach_list:
            data_t['total__contains'] = seach_list

        # 获取用户提交的信息
        Studentscore_dest = models.StudentScore.objects.filter(**data_t)
        for student in Studentscore_dest:
            total_score = student.language + student.maths + student.English + student.history + student.Geography + student.political
            student.total =total_score

        # 分页
        page_object = Pegination(request, Studentscore_dest, page_size=6)
        context = {
            'Studentscore_dest': page_object.page_mun,
            'page_string': page_object.html(),
            'seach_list': seach_list,
            'title': title,
            'form_data': form_data,
        }
        return render(request, "teacher_score.html", context)

#新建学生成绩信息
@csrf_exempt
def teacher_StudentScoreAdd(request):
    form_data=TeacherScoreModelForm(data=request.POST)
    if form_data.is_valid():
        form_data.save()
        data_dict={'static':True}
        return  JsonResponse(data_dict)
    return JsonResponse({'static':False,'error':form_data.errors})

#编辑窗口
def teacher_StudentScoreGET(request):
    #获取id
    uid=request.GET.get('uid')
    # 根据id获取学生成绩·表对应的那一列的数据
    score_Object=models.StudentScore.objects.filter(id=uid).values('StudentName','language','maths','English','history','Geography','political').first()

    #判断数据存不存在
    if not score_Object:
        return JsonResponse({'static':False,'error':'数据不存在'})
    result={'static':True,'data':score_Object}
    return JsonResponse(result)

#编辑学生成绩信息保存到数据库
@csrf_exempt
def teacher_StudentScoreEdit(request):
    #根据id获取课程信息表对应的那一列的数据
    uid=request.GET.get('uid')
    # 根据id获取课程信息表对应的那一列的数据
    leave_test=models.StudentScore.objects.filter(id=uid).first()
    if not leave_test:
        return JsonResponse({'static':False,'tpis':'数据不存在'})
    form_data=TeacherScoreModelForm(data=request.POST,instance=leave_test)
    if form_data.is_valid():
        form_data.save()
        return JsonResponse({'static':True})
    return HttpResponse(JsonResponse({'static':False,'error':form_data.errors}))

#删除学生成绩
def teacher_StudentScoreDelete(request):
      uid=request.GET.get('uid')
      exits=models.StudentScore.objects.filter(id=uid).exists()
      if not exits:
          return JsonResponse({'static':False,'error':'删除失败，数据不存在'})
      models.StudentScore.objects.filter(id=uid).delete()
      return JsonResponse({'static':True})


#教学评价信息管理
class RmationtableModelForm(BootrsapModelForm):
    class Meta:
        #关联表
        model=models.InformationTables
        fields='__all__'
#学生填写信息提交表单
class StudnetRmationtableModelForm(BootrsapModelForm):
    class Meta:
        #关联表
        model=models.StudentInformationTable
        exclude=['examRmation','adminRmation']
def teacher_Rmationlist(request):
    if request.method == 'GET':
        title = '教学评价信息收集表'
        test_data=RmationtableModelForm()
        form_data =StudnetRmationtableModelForm()
        # 搜索框
        seach_list = request.GET.get('q', '')
        data_t = {}
        # 搜索框中内容对应数据库中字段
        if seach_list:
            data_t['StudentName__contains'] = seach_list

        # 获取用户提交的信息
        rmation_dest = models.InformationTables.objects.all()
        student_dest=models.StudentInformationTable.objects.filter(**data_t)


        # 分页
        page_object = Pegination(request,student_dest, page_size=6)
        context = {
            'student_dest':page_object.page_mun,
            'rmation_dest': rmation_dest,
            'page_string': page_object.html(),
            'seach_list': seach_list,
            'title': title,
            'form_data': form_data,
            'test_data':test_data
        }
        return render(request, "teacher_Information.html", context)

#发布信息
@csrf_exempt
def teacher_RmationAdd(request):
    test_data=RmationtableModelForm(data=request.POST)
    if test_data.is_valid():
        test_data.save()
        data_dict={'static':True}
        return  JsonResponse(data_dict)
    return JsonResponse({'static':False,'error':test_data.errors})

#审核窗口
def teacher_RmationGET(request):
    #获取id
    uid=request.GET.get('uid')
    rmation_Object=models.StudentInformationTable.objects.filter(id=uid).values('id','StudentName','StudentID','sex','Texts','rmation','Filltime','teacherRmation','tables').first()

    #判断数据存不存在
    if not rmation_Object:
        return JsonResponse({'static':False,'error':'数据不存在'})
    result={'static':True,'data':rmation_Object}
    return JsonResponse(result)

#审核
@csrf_exempt
def teacher_RmationEdit(request):
    uid=request.GET.get('uid')
    rmation_test=models.StudentInformationTable.objects.filter(id=uid).first()
    if not rmation_test:
        return JsonResponse({'static':False,'tpis':'数据不存在'})
    form_data=StudnetRmationtableModelForm(data=request.POST,instance=rmation_test)
    if form_data.is_valid():
        form_data.save()
        return JsonResponse({'static':True})
    return HttpResponse(JsonResponse({'static':False,'error':form_data.errors}))

#删除发布信息
def teacher_RmationDelete(request):
      uid=request.GET.get('uid')
      exits=models.InformationTables.objects.filter(id=uid).exists()
      if not exits:
          return JsonResponse({'static':False,'error':'删除失败，数据不存在'})
      models.InformationTables.objects.filter(id=uid).delete()
      return JsonResponse({'static':True})

